home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __all__ = ('Capture',)
- import os
- import sys
- import signal
- import locale
- import subprocess
- import gobject
-
- class Capture(gobject.GObject):
- CAPTURE_STDOUT = 1
- CAPTURE_STDERR = 2
- CAPTURE_BOTH = 3
- __gsignals__ = {
- 'stdout-line': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
- 'stderr-line': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)),
- 'begin-execute': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, tuple()),
- 'end-execute': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT,)) }
-
- def __init__(self, command, cwd = None, env = { }):
- gobject.GObject.__init__(self)
- self.pipe = None
- self.env = env
- self.cwd = cwd
- self.flags = self.CAPTURE_BOTH
- self.command = command
- self.input_text = None
-
-
- def set_env(self, **values):
- self.env.update(**values)
-
-
- def set_command(self, command):
- self.command = command
-
-
- def set_flags(self, flags):
- self.flags = flags
-
-
- def set_input(self, text):
- self.input_text = text
-
-
- def set_cwd(self, cwd):
- self.cwd = cwd
-
-
- def execute(self):
- if self.command is None:
- return None
- popen_args = {
- 'cwd': self.cwd,
- 'shell': True,
- 'env': self.env }
- if self.input_text is not None:
- popen_args['stdin'] = subprocess.PIPE
-
- if self.flags & self.CAPTURE_STDOUT:
- popen_args['stdout'] = subprocess.PIPE
-
- if self.flags & self.CAPTURE_STDERR:
- popen_args['stderr'] = subprocess.PIPE
-
- self.pipe = subprocess.Popen(self.command, **popen_args)
- self.emit('begin-execute')
- if self.input_text is not None:
- self.pipe.stdin.write(self.input_text)
- self.pipe.stdin.close()
-
- if self.flags & self.CAPTURE_STDOUT:
- gobject.io_add_watch(self.pipe.stdout, gobject.IO_IN | gobject.IO_HUP, self.on_output)
-
- if self.flags & self.CAPTURE_STDERR:
- gobject.io_add_watch(self.pipe.stderr, gobject.IO_IN | gobject.IO_HUP, self.on_output)
-
- gobject.child_watch_add(self.pipe.pid, self.on_child_end)
-
-
- def on_output(self, source, condition):
- line = source.readline()
- if len(line) > 0:
-
- try:
- line = unicode(line, 'utf-8')
- except:
- line = unicode(line, locale.getdefaultlocale()[1], 'replace')
-
- if source == self.pipe.stdout:
- self.emit('stdout-line', line)
- else:
- self.emit('stderr-line', line)
- return True
- return False
-
-
- def stop(self, error_code = -1):
- if self.pipe is not None:
- os.kill(self.pipe.pid, signal.SIGTERM)
- self.pipe = None
-
-
-
- def on_child_end(self, pid, error_code):
- gobject.idle_add(self.emit, 'end-execute', error_code)
-
-
-